package com.jwb.pub.serviceImpl;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.model.JavaModel;
import com.jwb.pub.model.Page;
import com.jwb.pub.service.MsgService;
import com.jwb.util.ToolsUtil;
@Service
public class MsgServiceImpl implements MsgService
{
	@Autowired
	private BaseDAO baseDAO;
	
	
	/**
	 * 稿件来源为客户端 rmk10说明   客户端rmk10 为空   查找是否绑定了微博，微信，报端  用 客户端的 cjid 当条件 可以解绑
	 * 稿件来源为微信，微博，网站  rmk10 不为空 说明与客户端存在绑定关系 可以解绑，解绑后可以重新绑定
	 */
	@Override
	public JSONObject messageList(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		 String   sql="select msg_id,  "+
					"    msg_type,"+
					"     (select code_desc"+
					"        from user_dict u"+
					"        where dict_name = 'msg_type'"+
					"          and u.code = msg_type and u.sts='A') msg_type_name,"+
					"       msg_title,"+
					"       send_oper_code,"+
					"       (select real_name from user_info ui where ui.user_id = send_oper_code and ui.sts='A') send_oper_code_name,"+
					"       (select dept_id"+
					"          from dept d, user_info ui"+
					"         where ui.user_id = send_oper_code"+
					"           and d.dept_id = ui.cur_dept_id and d.dept_sts='A' and ui.sts='A') send_oper_dept_id,"+
					"       (select dept_name"+
					"          from dept d, user_info ui"+
					"         where ui.user_id = send_oper_code"+
					"           and d.dept_id = ui.cur_dept_id and d.dept_sts='A' and ui.sts='A') send_oper_dept_name,"+
					"       DATE_FORMAT(create_date, '%Y-%m-%d') create_date,"+
					"       msg_status,"+
					"       (select code_desc from user_dict u where  u.dict_name='msg_status' and u.code=msg_status and u.sts='A')	  msg_status_name,"+
					"       file_no,"+
					"       msg_cont,"+
					"       DATE_FORMAT(status_date, '%Y-%m-%d') status_date,"+
					"       idstr, "+
					"      gsbm,"+
					"       (select dept_name from dept d where d.dept_id=gsbm and d.dept_sts='A') gsbm_name "+
			        "  from msg m  where msg_status<>'msg_status_2' ";
				 String startDate="";
				 if (jsdata.containsKey("startDate"))
				 {
					 startDate=jsdata.getString("startDate");
					 
				 }
				 String endDate="";
				 if (jsdata.containsKey("endDate"))
				 {
					 endDate=jsdata.getString("endDate");
					 
				 }
				 
				 String msgType="";
				 if (jsdata.containsKey("msgType"))
				 {
					 msgType=jsdata.getString("msgType");
					 
				 }
				 String msgStatus="";
				 if (jsdata.containsKey("msgStatus"))
				 {
					 msgStatus=jsdata.getString("msgStatus");
					 
				 }
				 
				 
				 String msg_title="";
				 if (jsdata.containsKey("msg_title"))
				 {
					 msg_title=jsdata.getString("msg_title");
					 
				 }
				 
				 
				if(!"".equals(msg_title))  
		     	{
		     	
		    			sqlWhere+=" and m.msg_title like '%"+msg_title+"%' ";
		    		
		     	}
				
				String gsbm="";
				 if (jsdata.containsKey("gsbm"))
				 {
					 gsbm=jsdata.getString("gsbm");
					 
				 }
				 
				
			     if("-1".equals(gsbm)||"".equals(gsbm))
				 {
			    	  sqlWhere+=" and m.gsbm in ("+ToolsUtil.nestDeptSql(jsdata.getString("rankDeptId"), true)+")"; 

			     }
				 else
				 {
					 
				       sqlWhere+=" and m.gsbm in ("+ToolsUtil.nestDeptSql(gsbm, true)+")";

				 }
				 if (!"".equals(startDate) )
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(m.create_date,'%Y-%m-%d'),'%Y-%m-%d') >= "
							+ "STR_TO_DATE('" + startDate + " ','%Y-%m-%d')";
				}
				if (!"".equals(endDate))
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(m.create_date,'%Y-%m-%d'),'%Y-%m-%d') <= "
							+ "STR_TO_DATE('" + endDate + " ','%Y-%m-%d')";
				}
				
				 
			    
				if(!"".equals(msgType)&&!"-1".equals(msgType))
				{
					sqlWhere+=" and m.msg_type='"+msgType+"'";
				}
				
				if(!"".equals(msgStatus)&&!"-1".equals(msgStatus))
				{
					sqlWhere+=" and m.msg_status='"+msgStatus+"'";
				}
		  
			    sqlOrder=" order by create_date desc, msg_status, msg_type";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql,page);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}
	
	public JSONObject messageDel(JSONObject jsdata)	
	{
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		try
      	{      		
			String operType="";			
		    operType=jsdata.getString("operType");
		    String idstr="";			
		    idstr=jsdata.getString("idstr");	
		    String oper_code="";			
		    oper_code=jsdata.getString("oper_code");
		    String msgStatus="msg_status_2";			
		   
		    String sql="update msg"+
					"   set "+
					 "      send_oper_code = '"+oper_code+"',"+			
				    "       msg_status = '"+msgStatus+"',"+					
					"       status_date = now()"+						
					" where msg_id in ("+idstr+")" ; 
		    
		    boolean flag=baseDAO.excuteSql(sql, null);
		    
		    if(flag)
  			{
  				jsobject.put("respCode", "0");      				
             	jsobject.put("respDesc", "成功");
            
  			}
  			else
  			{
  				jsobject.put("respCode", "-1");
             	jsobject.put("respDesc", "数据保存失败");
  			}
      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}
			
	
		
		return jsobject;
	}
	
	
	public JSONObject messageSend(JSONObject jsdata)
	{
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		try
      	{      		
			String operType="";			
		    operType=jsdata.getString("operType");
		    String msgId="";			
		    msgId=jsdata.getString("msgId");	
		    String oper_code="";			
		    oper_code=jsdata.getString("oper_code");
		    String msgStatus="msg_status_1";			
		   
		    String sql="update msg"+
					"   set "+
					 "      send_oper_code = '"+oper_code+"',"+			
				    "       msg_status = '"+msgStatus+"',"+					
					"       status_date = now()"+						
					" where msg_id in ("+msgId+")" ; 
		    
		    boolean flag=baseDAO.excuteSql(sql, null);
		    
		    if(flag)
  			{
  				jsobject.put("respCode", "0");      				
             	jsobject.put("respDesc", "成功");
            
  			}
  			else
  			{
  				jsobject.put("respCode", "-1");
             	jsobject.put("respDesc", "数据保存失败");
  			}
      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}
			
	
		
		return jsobject;
	}
	
	
	
	public JSONObject msgDetailList(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		String   sql="SELECT detail_id,"+
						  "     m.msg_id,"+
						 "      recv_oper_code,"+
						 "      (SELECT real_name"+
						 "         FROM user_info ui"+
						 "        WHERE ui.user_id = m.recv_oper_code"+
						 "          AND ui.sts = 'A') recv_oper_code_name,"+
						 "      DATE_FORMAT(recv_date, '%Y-%m-%d') recv_date,"+
						 "      read_status,"+
						 "      (SELECT code_desc"+
						 "         FROM user_dict ud"+
						 "        WHERE ud.dict_name = 'read_status'"+
						 "          AND m.read_status = ud.code"+
						 "          AND ud.sts = 'A') read_status_name,"+
						 "      comm,"+
						 "      msg_type,"+
						 "      (SELECT code_desc"+
						 "         FROM user_dict u"+
						 "        WHERE dict_name = 'msg_type'"+
						 "          AND u.code = msg_type"+
						 "          AND u.sts = 'A') msg_type_name,"+
						 "      msg_title,"+
						 "      send_oper_code,"+
						 "      (SELECT real_name"+
						 "         FROM user_info ui"+
						 "        WHERE ui.user_id = send_oper_code"+
						 "          AND ui.sts = 'A') send_oper_code_name,"+
						 "      (SELECT real_name"+
						 "         FROM user_info ui"+
						 "        WHERE ui.user_id = RECV_OPER_CODE"+
						 "          AND ui.sts = 'A') recvOperName,"+
						 "      (SELECT dept_id"+
						 "         FROM dept d, user_info ui"+
						 "        WHERE ui.user_id = send_oper_code"+
						 "          AND d.dept_id = ui.cur_dept_id"+
						 "          AND d.dept_sts = 'A'"+
						 "          AND ui.sts = 'A') send_oper_dept_id,"+
						 "      (SELECT dept_name"+
						 "         FROM dept d, user_info ui"+
						 "        WHERE ui.user_id = send_oper_code"+
						 "          AND d.dept_id = ui.cur_dept_id"+
						 "          AND d.dept_sts = 'A'"+
						 "          AND ui.sts = 'A') send_oper_dept_name,"+
						 "      (SELECT dept_name"+
						 "         FROM dept d, user_info ui"+
						 "        WHERE ui.user_id = RECV_OPER_CODE"+
						 "          AND d.dept_id = ui.cur_dept_id"+
						 "          AND d.dept_sts = 'A'"+
						 "          AND ui.sts = 'A') recvDeptName,"+
						 "      DATE_FORMAT(create_date, '%Y-%m-%d') create_date,"+
						 "      msg_status,"+
						 "      (SELECT code_desc"+
						 "         FROM user_dict u"+
						 "        WHERE u.dict_name = 'msg_status'"+
						 "          AND u.code = msg_status"+
						 "          AND u.sts = 'A') msg_status_name,"+
						 "      file_no,"+
						 "      msg_cont,"+
						 "      idstr"+
						 " FROM msg_detail m, msg, user_info ut"+
						" WHERE 1 = 1"+
						"   AND m.recv_oper_code = ut.user_id"+
						"   AND ut.sts = 'A'"+
						"   AND msg.msg_status <> 'msg_status_2'"+
						"   AND m.msg_id = msg.msg_id";

				 String operType="";
				 if (jsdata.containsKey("operType"))
				 {
					 operType=jsdata.getString("operType");
					 
				 }
				 
				 String msgId="";
				 if (jsdata.containsKey("msgId"))
				 {
					 msgId=jsdata.getString("msgId");
					 
				 }
      		
	      		if("sendUser".equals(operType))
	        	{
	    	    	if (!"".equals(msgId) )
	    			{
	    	    		sqlWhere+=" and m.msg_id='"+msgId+"'";
	    			}	
	        	}
			    
	      		 String startDate="";
				 if (jsdata.containsKey("startDate"))
				 {
					 startDate=jsdata.getString("startDate");
					 
				 }
				 String endDate="";
				 if (jsdata.containsKey("endDate"))
				 {
					 endDate=jsdata.getString("endDate");
					 
				 }
				 if (!"".equals(startDate) )
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(m.create_date,'%Y-%m-%d'),'%Y-%m-%d') >= "
							+ "STR_TO_DATE('" + startDate + " ','%Y-%m-%d')";
				}
				if (!"".equals(endDate))
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(m.create_date,'%Y-%m-%d'),'%Y-%m-%d') <= "
							+ "STR_TO_DATE('" + endDate + " ','%Y-%m-%d')";
				}
			    sqlOrder=" order by read_status, create_date desc ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql,page);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}
	
	
	public JSONObject messageInfo(JSONObject jsdata) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		String   sql="select msg_id,  "+
					"    msg_type,"+
					"     (select code_desc"+
					"        from user_dict u"+
					"        where dict_name = 'msg_type'"+
					"          and u.code = msg_type and u.sts='A') msg_type_name,"+
					"       msg_title,"+
					"       send_oper_code,"+
					"       (select real_name from user_info ui where ui.user_id = send_oper_code and ui.sts='A') send_oper_code_name,"+
					"       (select dept_id"+
					"          from dept d, user_info ui"+
					"         where ui.user_id = send_oper_code"+
					"           and d.dept_id = ui.cur_dept_id and d.dept_sts='A' and ui.sts='A') send_oper_dept_id,"+
					"       (select dept_name"+
					"          from dept d, user_info ui"+
					"         where ui.user_id = send_oper_code"+
					"           and d.dept_id = ui.cur_dept_id and d.dept_sts='A' and ui.sts='A') send_oper_dept_name,"+
					"       DATE_FORMAT(create_date, '%Y-%m-%d') create_date,"+
					"       msg_status,"+
					"       (select code_desc from user_dict u where  u.dict_name='msg_status' and u.code=msg_status and u.sts='A')	  msg_status_name,"+
					"       file_no,"+
					"       msg_cont,"+
					"       DATE_FORMAT(status_date, '%Y-%m-%d') status_date,"+
					"       idstr, "+
					"      gsbm,"+
					"       (select dept_name from dept d where d.dept_id=gsbm and d.dept_sts='A') gsbm_name "+
			        "  from msg m  where msg_status<>'msg_status_2' ";
				 String msg_id="";
				 if (jsdata.containsKey("msg_id"))
				 {
					 msg_id=jsdata.getString("msg_id");
					 
				 }			
			    
				if(!"".equals(msg_id))
				{
					sqlWhere+=" and m.msg_id="+msg_id;
				}	
				
			    sqlOrder=" ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}
	
	public JSONObject messageSave(JSONObject jsdata)
	{
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		try
      	{      		
			String operType=!jsdata.containsKey("operType") ? "" : jsdata.getString("operType");
			String msgId=!jsdata.containsKey("msgId") ? "" : jsdata.getString("msgId");
			String createDate=!jsdata.containsKey("createDate") ? "" : jsdata.getString("createDate");
			String msgType=!jsdata.containsKey("msgType") ? "" : jsdata.getString("msgType");
			String msgTitle=!jsdata.containsKey("msgTitle") ? "" : jsdata.getString("msgTitle");
			String fileNo=!jsdata.containsKey("fileNo") ? "" : jsdata.getString("fileNo");
			String msgCont=!jsdata.containsKey("msgCont") ? "" : jsdata.getString("msgCont");
			String gsbm=!jsdata.containsKey("gsbm") ? "" : jsdata.getString("gsbm");
			String oper_code=!jsdata.containsKey("oper_code") ? "" : jsdata.getString("oper_code");	
			String recvs = !jsdata.containsKey("recvs")?"":jsdata.getString("recvs");
			String sql="";
			String msgStatus="msg_status_0";	//改写成batch
			
			LinkedHashMap<String,ArrayList<JavaModel>> map = new LinkedHashMap<String,ArrayList<JavaModel>>();
			
			if("add".equals(operType))
    		{        			
    			//未发布添加
			
        		sql="insert into msg"+
                		"	      (msg_id,"+
                		"			   msg_type,"+
                		"			   msg_title,"+
                		"			   send_oper_code,"+
                		"			   create_date,"+
                		"			   msg_status,"+
                		"			   file_no,"+
                		"			   status_date,"+
                		"			   msg_cont,"+
                		"			   idstr,"+
                		"			   gsbm,"+
                		"			   msg_path)"+
                		"			values"+
                		"			  ("+msgId+","+
                		"			   '"+msgType+"',"+
                		"			   '"+msgTitle+"',"+
                		"			   '"+oper_code+"',"+
                		"			   STR_TO_DATE('"+createDate+"','%Y-%m-%d'),"+
                		"			   '"+msgStatus+"',"+
                		"			   '"+fileNo+"',"+
                		"			   NOW(),"+
                		"			   ?,"+                	
                		"			   NULL, "+
                		"			   '"+gsbm+"',"+
                		"			   NULL)";
        		
    		}
			else if("edit".equals(operType))
    		{
    			//未发布 修改
    			sql="update msg"+
						"   set "+
						"       msg_type = '"+msgType+"',"+
						"       msg_title = '"+msgTitle+"',"+
						"       send_oper_code = '"+oper_code+"',"+
						"       create_date =  STR_TO_DATE('"+createDate+"','%Y-%m-%d'),"+
						"       msg_status = '"+msgStatus+"',"+
						"       file_no = '"+fileNo+"',"+
						"       status_date = NOW(),"+
						"       msg_cont = ?,"+
						"       idstr = NULL,"+
						"       gsbm = '"+gsbm+"',"+
						"       msg_path = NULL"+
						" where msg_id = "+msgId;
    		}
			ArrayList<JavaModel> list = new ArrayList<JavaModel>();
  			list = new ArrayList<JavaModel>();
  			list.add(new JavaModel(msgCont));
  			map.put(sql, list);
		    sql="delete from msg_detail where msg_id=? ";
  			list = new ArrayList<JavaModel>();
  			list.add(new JavaModel(msgId));
  			map.put(sql, list);
  			List<String> recv_codes = new ArrayList<>();
  			for(String str:recvs.split(",")){
  				if(str.endsWith("_p")){
  					recv_codes.add(str.substring(0,str.indexOf("_p")));
  				}  					
  			}
  			sql ="insert into msg_detail (msg_id,recv_oper_code,recv_date,comm) values";
  			for(int i=0;i<recv_codes.size();i++){
  				sql += "(?,?,NOW(),null)";
  				if(i!=recv_codes.size()-1){
  					sql +=",";
  				}
  			}
  			list = new ArrayList<>();
  			for(int i=0;i<recv_codes.size();i++){
  				list.add(new JavaModel(msgId));
  				list.add(new JavaModel(recv_codes.get(i)));  				
  			}
  			map.put(sql, list);
  			boolean flag = baseDAO.excuteBatchOrderedSql(map);
  			
  			if(!flag){
  				jsobject.put("respCode", "-1");
  	           	jsobject.put("respDesc", "更新消息接收人失败");  
  	           	return jsobject;
  			} 
  			
  			jsobject.put("respCode", "0");      				
  	        jsobject.put("respDesc", "成功");
  			      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}
			
	
		
		return jsobject;
	}
	
	
	public JSONObject msgDetailDel(JSONObject jsdata)	
	{
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		try
      	{      		
			String detail_id="";			
			detail_id=jsdata.getString("detail_id");		    	
		   
		    String sql="delete from msg_detail WHERE detail_id="+detail_id;
					
		    
		    boolean flag=baseDAO.excuteSql(sql, null);
		    
		    if(flag)
  			{
  				jsobject.put("respCode", "0");      				
             	jsobject.put("respDesc", "成功");
            
  			}
  			else
  			{
  				jsobject.put("respCode", "-1");
             	jsobject.put("respDesc", "数据保存失败");
  			}
      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}
			
	
		
		return jsobject;
	}
	
	
	
	public JSONObject revMessageList(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		 String   sql="select m.msg_id,  "+
      				"    detail_id,"+
					"    msg_type,"+
					"     (select code_desc"+
					"        from user_dict u"+
					"        where dict_name = 'msg_type'"+
					"          and u.code = msg_type and u.sts='A') msg_type_name,"+
					"       msg_title,"+	
					"       DATE_FORMAT(create_date, '%Y-%m-%d') create_date,"+
					"       read_status,"+
					"       (select code_desc from user_dict u where  u.dict_name='read_status' and u.code=read_status and u.sts='A')	  read_status_name "+
			        "  from msg m ,msg_detail md  where msg_status = 'msg_status_1' and m.msg_id = md.msg_id ";
				 String startDate="";
				 if (jsdata.containsKey("startDate"))
				 {
					 startDate=jsdata.getString("startDate");
					 
				 }
				 String endDate="";
				 if (jsdata.containsKey("endDate"))
				 {
					 endDate=jsdata.getString("endDate");
					 
				 }
				 
				 String msgType="";
				 if (jsdata.containsKey("msgType"))
				 {
					 msgType=jsdata.getString("msgType");
					 
				 }
				 String read_status="";
				 if (jsdata.containsKey("read_status"))
				 {
					 read_status=jsdata.getString("read_status");
					 
				 }
				 
				 
				 String msg_title="";
				 if (jsdata.containsKey("msg_title"))
				 {
					 msg_title=jsdata.getString("msg_title");
					 
				 }
				 
				 
				if(!"".equals(msg_title))  
		     	{
		     	
		    			sqlWhere+=" and m.msg_title like '%"+msg_title+"%' ";
		    		
		     	}
				
			
				 if (!"".equals(startDate) )
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(m.create_date,'%Y-%m-%d'),'%Y-%m-%d') >= "
							+ "STR_TO_DATE('" + startDate + " ','%Y-%m-%d')";
				}
				if (!"".equals(endDate))
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(m.create_date,'%Y-%m-%d'),'%Y-%m-%d') <= "
							+ "STR_TO_DATE('" + endDate + " ','%Y-%m-%d')";
				}
				
				 
			    
				if(!"".equals(msgType)&&!"-1".equals(msgType))
				{
					sqlWhere+=" and m.msg_type='"+msgType+"'";
				}
				
				if(!"".equals(read_status)&&!"-1".equals(read_status))
				{
					sqlWhere+=" and md.read_status='"+read_status+"'";
				}
		  
				 String recv_oper_code="";
				 if (jsdata.containsKey("recv_oper_code"))
				 {
					 recv_oper_code=jsdata.getString("recv_oper_code");
					 
				 }
				 
				 
				if(!"".equals(recv_oper_code))  
		     	{
		     	
		    			sqlWhere+=" and md.recv_oper_code = '"+recv_oper_code+"' ";
		    		
		     	}
				
			    sqlOrder=" order by md.read_status ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql,page);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}

	@Override
	public JSONObject userReadMsg(JSONObject jsdata) {
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");	
		String detail_id=!jsdata.containsKey("detail_id") ? "" : jsdata.getString("detail_id");	
		try
      	{      		
				
			detail_id=jsdata.getString("detail_id");		
			String sqlStr="select count(*) cnt from msg_detail where read_status='read_status_0' and detail_id='"+detail_id+"'";
			int cnt=baseDAO.findBySqlCnt(sqlStr);
			if(cnt==0)
			{
				jsobject.put("respCode", "0");      				
             	jsobject.put("respDesc", "已阅读");
				return jsobject;
			}			
		    String sql="update msg_detail "+
		    		   "set  "+		    		 
		    		   "  recv_date = now(),"+
		    		   "  read_status = 'read_status_1' "+   		      
		    		" where detail_id = '"+detail_id+"'";
		    
		    boolean flag=baseDAO.excuteSql(sql, null);
		    
		    if(flag)
  			{
  				jsobject.put("respCode", "0");      				
             	jsobject.put("respDesc", "成功");
            
  			}
  			else
  			{
  				jsobject.put("respCode", "-1");
             	jsobject.put("respDesc", "失败");
  			}
      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}	
		
		return jsobject;
	}
	
	
	
	@Override
	public JSONObject userCommMsg(JSONObject jsdata) {
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		String detail_id=!jsdata.containsKey("detail_id") ? "" : jsdata.getString("detail_id");		
		String comm=!jsdata.containsKey("comm") ? "" : jsdata.getString("comm");
		try
      	{      		
			
			String sql="update msg_detail "+
		    		   "set  "+
		    		   "    comm = '"+comm+"',"+
		    		   "    recv_date = NOW(),"+
		    		   "    read_status = 'read_status_1' "+   		      
		    		" where detail_id = '"+detail_id+"'";
		    
		    boolean flag=baseDAO.excuteSql(sql, null);
		    
		    if(flag)
  			{
  				jsobject.put("respCode", "0");      				
             	jsobject.put("respDesc", "成功");
            
  			}
  			else
  			{
  				jsobject.put("respCode", "-1");
             	jsobject.put("respDesc", "失败");
  			}
      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}	
		
		return jsobject;
	}
	
	
	
	public JSONObject msgDetailInfo(JSONObject jsdata) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		 String   sql="select m.msg_id,  "+
      				"    detail_id,"+
					"    msg_type,"+
					"     (select code_desc"+
					"        from user_dict u"+
					"        where dict_name = 'msg_type'"+
					"          and u.code = msg_type and u.sts='A') msg_type_name,"+
					"       send_oper_code,"+
					"       (select real_name from user_info ui where ui.user_id = send_oper_code and ui.sts='A') send_oper_code_name,"+
					"       (select dept_id"+
					"          from dept d, user_info ui"+
					"         where ui.user_id = send_oper_code"+
					"           and d.dept_id = ui.cur_dept_id and d.dept_sts='A' and ui.sts='A') send_oper_dept_id,"+
					"       (select dept_name"+
					"          from dept d, user_info ui"+
					"         where ui.user_id = send_oper_code"+
					"           and d.dept_id = ui.cur_dept_id and d.dept_sts='A' and ui.sts='A') send_oper_dept_name,"+
					"       msg_title,"+	
					"       DATE_FORMAT(create_date, '%Y-%m-%d') create_date,"+
					"       read_status,"+
					"       file_no,"+
					"       msg_cont,"+
					"       (select code_desc from user_dict u where  u.dict_name='read_status' and u.code=read_status and u.sts='A')	  read_status_name, "+
					"       idstr, "+
					"      gsbm,"+
					"      comm,"+
					"       (select dept_name from dept d where d.dept_id=gsbm and d.dept_sts='A') gsbm_name "+
					"  from msg m ,msg_detail md  where msg_status = 'msg_status_1' and m.msg_id = md.msg_id ";
				
				 
				 String detail_id="";
				 if (jsdata.containsKey("detail_id"))
				 {
					 detail_id=jsdata.getString("detail_id");
					 
				 }
				
			
				if(!"".equals(detail_id))
				{
					sqlWhere+=" and md.detail_id='"+detail_id+"'";
				}
				
				
				
			    sqlOrder="  ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}
	
	
	
	
	
	
	public JSONObject msgList(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		 String   sql="select m.msg_id,  "+
      				"    detail_id,"+
					"    msg_type,"+
					"     (select code_desc"+
					"        from user_dict u"+
					"        where dict_name = 'msg_type'"+
					"          and u.code = msg_type and u.sts='A') msg_type_name,"+
					"       msg_title,"+	
					 "      msg_cont,"+
					"       DATE_FORMAT(create_date, '%Y-%m-%d') create_date,"+
					"       read_status,"+
					"       (select code_desc from user_dict u where  u.dict_name='read_status' and u.code=read_status and u.sts='A')	  read_status_name "+
			        "  from msg m ,msg_detail md  where msg_status = 'msg_status_1' and m.msg_id = md.msg_id ";
				
				 String read_status="";
				 if (jsdata.containsKey("read_status"))
				 {
					 read_status=jsdata.getString("read_status");
					 
				 }
				 
				 
				 
				if(!"".equals(read_status)&&!"-1".equals(read_status))
				{
					sqlWhere+=" and md.read_status='"+read_status+"'";
				}
		  
				 String recv_oper_code="";
				 if (jsdata.containsKey("recv_oper_code"))
				 {
					 recv_oper_code=jsdata.getString("recv_oper_code");
					 
				 }
				 
				 
				if(!"".equals(recv_oper_code))  
		     	{
		     	
		    			sqlWhere+=" and md.recv_oper_code = '"+recv_oper_code+"' ";
		    		
		     	}
				
			    sqlOrder=" order by md.read_status ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql,page);		
			    
			    
			       sql="select count(*) cnt "+
				        "  from msg m ,msg_detail md  where msg_status = 'msg_status_1' and m.msg_id = md.msg_id ";
					
					  read_status="read_status_0";	 
					 
					 
					if(!"".equals(read_status)&&!"-1".equals(read_status))
					{
						sqlWhere+=" and md.read_status='"+read_status+"'";
					}
			  
					  recv_oper_code="";
					 if (jsdata.containsKey("recv_oper_code"))
					 {
						 recv_oper_code=jsdata.getString("recv_oper_code");
						 
					 }
					 
					 
					if(!"".equals(recv_oper_code))  
			     	{
			     	
			    			sqlWhere+=" and md.recv_oper_code = '"+recv_oper_code+"' ";
			    		
			     	}
					
				    sqlOrder="  ";
				    sql+=sqlWhere+sqlOrder;	       
				    int unReadCnt=baseDAO.findBySqlCnt(sql);		
			    
			    
			        jsobject.put("unReadCnt", unReadCnt);
			    
			   
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}
}
